/**
 * Created by Administrator on 2019/3/25.
 */
$(function () {
    initComponent();
    clearTheForm();
    addNewRole();
});

var initComponent = function () {
    var sign = [];
    sign.push({
        "signVal":"superadmin",
        "signName":"超级管理员"
    });
    sign.push({
        "signVal":"admin",
        "signName":"管理员"
    });
    sign.push({
        "signVal":"staff",
        "signName":"员工"
    });
    sign.unshift({
        "signVal":"staff",
        "signName":"默认"
    });
    $('#sign').combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'signVal',
        textField : 'signName',
        data: sign
    });

    var status = [];
    status.push({
        "statusVal":"on",
        "statusName":"启用"
    });
    status.push({
        "statusVal":"off",
        "statusName":"停用"
    });
    $('#status').combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'statusVal',
        textField : 'statusName',
        data: status
    });
}

// 清空表单
var clearTheForm = function () {
    $("#clear").click(function () {
        $('#addRolesForm').form('clear');
    });
}

var addNewRole = function () {
    $("#save").click(function () {
        if($("input[name='roleName']").val()=="") {
            showDialogWithContent("请输入角色名称！");
            return;
        } else if($("input[name='sign']").val()==""){
            showDialogWithContent("请选择角色标识！");
            return;
        } else if($("input[name='status']").val()==""){
            showDialogWithContent("请选择状态！");
            return;
        }
        var params = new Object();
        params.roleName = $("input[name='roleName']").val();
        params.sign = $("input[name='sign']").val();
        params.status = $("input[name='status']").val();
        params.described = $("input[name='described']").val();
        params.remark = $("input[name='remark']").val();
        var jsonstr = postSyncAjax("/addNewRole/normal", params);
        //console.log("测试：");
        //console.log("测试："+jsonstr);
        if(jsonstr.success) {
            $.messager.alert("消息提示","添加成功！","info");
            //showDialogWithContent("添加成功！");
        } else {
            if (jsonstr.status == 401) {
                $.messager.alert("消息提示",jsonstr.msg,"warning",
                    function () {
                        window.top.location.href = "/index";
                    });
                /*showDialogSetTime(jsonstr.msg, function () {
                 window.top.location.href = "/index";
                 }, 5000);*/
            }
            $.messager.alert("消息提示","添加失败：" + jsonstr.msg,"warning");
            //showDialogWithContent("添加失败：" + jsonstr.msg);
            return;
        }
    });
}